#include"StdAfx.h"

double g_fLastFrameTime = 0;

pgTimer::pgTimer()
{
	m_fAverageFPS = 0.0f;
}

pgTimer::~pgTimer()
{
}

void pgTimer::Update()
{
	QueryPerformanceCounter((LARGE_INTEGER*)&m_pCurCount);
	m_llElapsed = m_pCurCount - m_pLastCount;
	m_pLastCount = m_pCurCount;

	m_fLastFrameTime = (float)m_llElapsed / (double)m_pFrequencyCount;

	m_fAverageFPS += 1.0 / m_fLastFrameTime;
	m_fAverageFPS /= 2.0f;
}

void pgTimer::Init()
{
	PG_LOG_ALWAYS_EX(TOPIC_PGTIMER, "Initializing timer...");

	if (!QueryPerformanceFrequency((LARGE_INTEGER*)&m_pFrequencyCount))
		PG_LOG_WARNING_EX(TOPIC_PGTIMER, "Failed to query performance frequency!");
	if (!QueryPerformanceCounter((LARGE_INTEGER*)&m_pLastCount))
		PG_LOG_WARNING_EX(TOPIC_PGTIMER, "Failed to query performance counter!");

	m_fLastFrameTime = 0.0f;	

	PG_LOG_ALWAYS_EX(TOPIC_PGTIMER, "Initialization successful!");
}